package Test2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {

	public static void main(String[] args) {
		String filename = "/Export/EC-T11-2013-0310-GS-JG-01_24-00-T11-5000010LV_20130806170925/CAD_T11-5000010LV-01-20130806174009/T11-8403901_02.CATPart";
		System.out.println(checkFileExt(filename));
	}

	public static boolean checkFileExt(String fileName) {
		fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
		String reg = "CATProduct|CATPart|xls";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(fileName);
		return m.matches();
	}

	public static void t1(String comnet) {
		String reg = "^The";// 开头要有The
		boolean is = find(reg, comnet);
		System.out.println(is);
	}

	public static void t2(String comnet) {
		String reg = "a.[0-9]";// 一个a加一个.再加一个0到9的数字
		boolean is = find(reg, comnet);
		System.out.println(is);
	}

	public static void t3(String str) {
		String regex = "\\b\\w+\\b"; // 单词以空格分割
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}

	public static void t4(String comnet) {
		String reg = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.),(([\\w-]+\\.)+))([a-zA-Z]{2,4},[0-9]{1,3})(\\]?)$";// 
		boolean is = matcher(reg, comnet);
		System.out.println(is);
	}

	// 1.如果目标文本第一个字符是单词字符，则匹配第一个字符之前的位置
	// 2.如果目标文本最后一个字符是单词字符，则匹配最后一个字符之后的位置
	// 3.如果两个连续的字符中一个是单词字符一个不是，那么匹配中间位置
	public static void t5(String comnet) {
		String reg = "\\bcat\\b";// t5("cat iamcat");
		boolean is = find(reg, comnet);
		System.out.println(is);
	}

	public static void t6(String comnet) {
		String reg = "\\b(?!cat\\b)\\w+\\b";// 查找除了某个单词之外的任意单词，如：查找除了cat之外的任意其他单词
		boolean is = find(reg, comnet);
		System.out.println(is);
	}

	public static void t7(String str) {
		String regex = "\\bis\\b"; // this is a cat
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}

	// 查找后面不跟着某个特定单词的任意单词，如：查找后面不跟着cat的任意单词，可以忽略其中空格和标点和非单词字符
	// 正则：\b\w+\b(?!\W+cat\b)
	public static boolean find(String reg, String comnet) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(comnet);
		boolean is = m.find();
		return is;
	}

	public static boolean matcher(String reg, String comnet) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(comnet);
		boolean is = m.matches();
		return is;
	}
}
